package com.hulk.hyena.project.monitor.job.service;


import com.hulk.hyena.common.constant.ScheduleConstants;
import com.hulk.hyena.common.exception.job.TaskException;
import com.hulk.hyena.framework.quartz.utils.QuartzManager;
import com.hulk.hyena.framework.quartz.utils.ScheduleJobUtils;
import com.hulk.hyena.project.monitor.job.domain.ScheduleJob;
import com.hulk.hyena.project.monitor.job.domain.Task;
import com.hulk.hyena.project.monitor.job.mapper.TaskMapper;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JobTaskServiceImpl implements IJobTaskService {

	@Autowired
	private Scheduler scheduler;

	@Autowired
	private TaskMapper taskScheduleJobMapper;

	@Autowired
	QuartzManager quartzManager;

	@Override
	public Task get(Long id) {
		return taskScheduleJobMapper.get(id);
	}

	@Override
	public List<Task> list(Task task) {
		return taskScheduleJobMapper.list(task);
	}



	@Override
	public int save(Task taskScheduleJob) {
		return taskScheduleJobMapper.save(taskScheduleJob);
	}

	@Override
	public int update(Task taskScheduleJob) {
		return taskScheduleJobMapper.update(taskScheduleJob);
	}

	@Override
	public int remove(Long id) {
		try {
			Task scheduleJob = get(id);
			quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
			return taskScheduleJobMapper.remove(id);
		} catch (TaskException e) {
			e.printStackTrace();
			return 0;
		}

	}

	@Override
	public int batchRemove(Long[] ids) {
		for (Long id : ids) {
			try {
				Task scheduleJob = get(id);
				quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
			} catch (TaskException e) {
				e.printStackTrace();
				return 0;
			}
		}
		return taskScheduleJobMapper.batchRemove(ids);
	}

	@Override
	public void initSchedule()  {
		// 这里获取任务信息数据
		List<Task> jobList = taskScheduleJobMapper.list(new Task());
		for (Task scheduleJob : jobList) {
			if ("1".equals(scheduleJob.getJobStatus())) {
				ScheduleJob job = ScheduleJobUtils.entityToData(scheduleJob);
				quartzManager.addJob(job);
			}

		}
	}

	@Override
	public int changeStatus(Task job) {

		int resultFlag = 0 ;
		Long jobId  =job.getId();
		String cmd = job.getJobStatus();

		Task scheduleJob = get(jobId);
		if (scheduleJob == null) {
			return resultFlag;
		}
		scheduleJob.setJobStatus(cmd);
		if (ScheduleConstants.STATUS_RUNNING_STOP.equals(cmd)) {
			try {
				quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
			} catch (TaskException e) {
				e.printStackTrace();
				return 0;
			}
			scheduleJob.setJobStatus(ScheduleJob.STATUS_NOT_RUNNING);
		} else {
			if (!ScheduleConstants.STATUS_RUNNING_START.equals(cmd)) {
			} else {
                scheduleJob.setJobStatus(ScheduleJob.STATUS_RUNNING);
                quartzManager.addJob(ScheduleJobUtils.entityToData(scheduleJob));
            }
		}
		resultFlag = update(scheduleJob);
		return resultFlag ;
	}

	@Override
	public int runAJobNow(Task job) {
		int resultFlag = 0 ;
		Long jobId  =job.getId();
		//String cmd = job.getJobStatus();

		Task scheduleJob = get(jobId);
		//scheduleJob.setJobStatus(cmd);
		if (scheduleJob == null) {
			return resultFlag;
		}

		quartzManager.runAJobNow(ScheduleJobUtils.entityToData(scheduleJob));

		resultFlag = update(scheduleJob);
		return resultFlag;
	}
	@Override
	public int runNow(Task job) {
		return quartzManager.runNow(scheduler, get(job.getId()));
	}

	@Override
	public int updateCron(Task scheduleJob)  {
		int resultFlag = 0 ;

		if (scheduleJob == null) {
			return resultFlag;
		}
		if(ScheduleJob.STATUS_RUNNING.equals(scheduleJob.getJobStatus())) {
			quartzManager.updateJob(ScheduleJobUtils.entityToData(scheduleJob));

		} else {
			quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
		}
		resultFlag = update(scheduleJob);
		return resultFlag;
	}

}
